/*
 * Copyright (C) 2014 Realtek Semiconductor Corp.
 * All Rights Reserved.
 *
 * This program is the proprietary software of Realtek Semiconductor
 * Corporation and/or its licensors, and only be used, duplicated,
 * modified or distributed under the authorized license from Realtek.
 *
 * ANY USE OF THE SOFTWARE OTHER THAN AS AUTHORIZED UNDER
 * THIS LICENSE OR COPYRIGHT LAW IS PROHIBITED.
 *
 * Purpose :
 *
 * Feature : The file includes the following modules and sub-modules
 *           (1)
 */
#include "app_basic.h"

#include "feature_export_api.h"

static signed customOpt = -1; /*save as bit mask */

#define API_OPT_IGN_ME_171_DEFAULT_DROP (0) /* bit 0 */
#define API_OPT_IGN_ME_171_TREAT_UNTAG  (1) /* bit 1 */
#define API_OPT_IGN_ME_171_ALL			(2) /* bit 2 */
#define API_OPT_ADD_ME_171_DEFAULT_RULE	(3) /* bit 3 */


GOS_ERROR_CODE omci_ingoreExtVlanDefaultDrop(va_list argp)
{
    BOOL    flag = FALSE;
    UINT32  custom_bm = 0, ret = FAL_FAIL;
    MIB_TABLE_VLANTAGFILTERDATA_T *ptr = NULL;
	MIB_TREE_CONN_T	*pConn  = va_arg(argp, MIB_TREE_CONN_T *);
	int	index       = va_arg(argp, int);
    omci_vlan_rule_t *pRule = va_arg(argp, omci_vlan_rule_t *);
    va_end(argp);

    if (-1 == customOpt)
        custom_bm = 1;
    else
        custom_bm = customOpt;

    if ((1 << API_OPT_IGN_ME_171_ALL) & custom_bm)
    {
    	if(pConn->pAniVlanTagFilter || pConn->pUniVlanTagFilter)
    		omci_GenVlanTagFilterRule(pConn,index, TRUE);
    	else
    		omci_GenNoVlanFilterRule(pConn,index, TRUE);
    	ret = FAL_OK;
    	OMCI_LOG(OMCI_LOG_LEVEL_DBG, "%s()%d: IGN_ME_171_ALL", __FUNCTION__, __LINE__);
    }
    else
    {

        if (!pRule && ((1 << API_OPT_ADD_ME_171_DEFAULT_RULE) & custom_bm))
        {
            omci_GenExtVlanDefaultRule(pConn,index);
            return GOS_ERR_STATE;
        }


		if (!pRule)
    		return ret;

    	if ((1 << API_OPT_IGN_ME_171_DEFAULT_DROP) & custom_bm)
    	{
        	if (pRule->vlanRule.outStyle.isDefaultRule == OMCI_EXTVLAN_REMOVE_TAG_DISCARD)
        	{
            	if(pConn->pAniVlanTagFilter || pConn->pUniVlanTagFilter)
                	omci_GenVlanTagFilterRule(pConn,index, TRUE);
            	else
                	omci_GenNoVlanFilterRule(pConn,index, TRUE);
            	ret = FAL_OK;
            	flag = TRUE;
            	OMCI_LOG(OMCI_LOG_LEVEL_DBG, "%s()%d: IGN_ME_171_DEFAULT_DROP", __FUNCTION__, __LINE__);
        	}
    	}

    	if ((1 << API_OPT_IGN_ME_171_TREAT_UNTAG) & custom_bm)
    	{
        	ptr = pConn->pAniVlanTagFilter;
        	if (PON_GEMPORT_DIRECTION_DS != pRule->dir &&
            	pRule->vlanRule.outStyle.outTagNum == 0 &&
            	NULL != ptr &&
            	0 != ptr->NumOfEntries)
        	{
            	if (!flag)
            	{
                	if(pConn->pAniVlanTagFilter || pConn->pUniVlanTagFilter)
                    	omci_GenVlanTagFilterRule(pConn,index, TRUE);
                	else
                    	omci_GenNoVlanFilterRule(pConn,index, TRUE);
            	}
            	ret = FAL_OK;
            	OMCI_LOG(OMCI_LOG_LEVEL_DBG, "%s()%d: IGN_ME_171_TREAT_UNTAG", __FUNCTION__, __LINE__);
        	}
    	}

    	OMCI_LOG(OMCI_LOG_LEVEL_DBG, "%s()%d, ret=%u customOpt=%d, custom_bm=%u", __FUNCTION__, __LINE__, ret, customOpt, custom_bm);
	}
    return ret;

}

unsigned int feature_module_opt_init(unsigned int moduleID, ... )
{

    va_list argPtr;

    va_start(argPtr, moduleID);
    customOpt = va_arg(argPtr, signed);
    va_end (argPtr);

    if (FAL_OK != feature_api_register(FEATURE_API_BDP_00000080, moduleID, &omci_ingoreExtVlanDefaultDrop))
        return FAL_FAIL;

    return FAL_OK;
}


unsigned int feature_module_exit(void)
{
	feature_api_unregister(FEATURE_API_BDP_00000080);

    return FAL_OK;
}

